/*
* Copyright 2016 YONYOU Corporation Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project         marketing

* @Package         com.yonyou.marketing.message.push.resolver

* @Author          LuZhen

* @FileName        MessageResolverImpl

* @Date            16/11/3

----------------------------------------------------------------------------------
*     Date          Who             Version         Comments
* 1. 16/11/3        LuZhen          1.0             Create Source
*
----------------------------------------------------------------------------------
*/
package com.yonyou.gmmc.service.wechatpush.resolver;

import org.apache.log4j.Logger;
import org.marketing.common.constants.MessagePushConstant.*;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocol;
import org.marketing.common.util.PushCommonUtils;
import org.springframework.stereotype.Service;

@Service
public class MessageResolverImpl implements MessageResolver {

    private static Logger logger = Logger.getLogger(MessageResolverImpl.class);

    @Override
    public PushMessage receiveMessage(WeChatMessageProtocol messageProtocol) {
        PushMessage pushMessage = new PushMessage(messageProtocol);
        return pushMessage;
    }

//    /**
//     * @param origin    origin
//     * @return  boolean
//     */
//    private boolean checkWebChatCustomMessageProtocol(WeChatMessageProtocol origin){
//        //detail receiver
//        if(!origin.getReceiver().isDetail()){
//            logger.error("web chat custom message only support push to some detail person ");
//            return false;
//        }
//        if(PushCommonUtils.isNull(origin.getReceiver().getDetailList())){
//            logger.error("web chat custom message need detail person list");
//            return false;
//        }
//        if(origin.getReceiver().getDetailList().size()>1){
//            logger.error("web chat custom message only support one person now");
//            return false;
//        }
//        //content type
//        if(origin.getMessage()==null){
//            logger.error("web chat custom message need message content ");
//            return false;
//        }
//        if(origin.getMessage().getContentType()==null){
//            logger.error("web chat custom message need message content type");
//            return false;
//        }
//        //check content type according to api
//        switch(origin.getMessage().getContentType()){
//            case    VIDEO:
//                if(!origin.getMessage().getExtraParaMap().containsKey(WX_MSG_PROP_THUMB)){
//                    logger.error("web chat custom video message must have property THUMB");
//                    return false;
//                }
//                if(!origin.getMessage().getExtraParaMap().containsKey(WX_MSG_PROP_DESC)){
//                    logger.error("web chat custom video message must have property description");
//                    return false;
//                }
//            case    NEWS:
//                if(!PushCommonUtils.isNull(origin.getMessage().getLinkContentList())){
//                    logger.error("web chat custom news message must have articles ");
//                    return false;
//                }
//            default:
//                break;
//        }
//        return true;
//    }


    /**
     * 检查微信公众推送消息参数
     * @param origin    origin
     * @return  boolean
     */
    private boolean checkWebChatPublicMessageProtocol(WeChatMessageProtocol origin){
        //detail receiver
        if(!origin.getReceiver().isDetail()){
            logger.error("web chat template message only support push to some detail person ");
            return false;
        }
        if(PushCommonUtils.isNull(origin.getReceiver().getDetailList())){
            logger.error("web chat template message need detail person list");
            return false;
        }
        if(origin.getSender()==null||PushCommonUtils.isNull(origin.getSender().getClientAppId())){
            logger.error("web chat sender appClientId error");
            return false;
        }
//        if(origin.getReceiver().getDetailList().size()>1){
//            logger.error("web chat template message only support one person now");
//            return false;
//        }
        //content type
        if(origin.getMessage()==null){
            logger.error("web chat template message need message content ");
            return false;
        }
        //other template content
//        if(PushCommonUtils.isNull(origin.getMessage().getTemplateConentList())){
//            logger.error("web chat template message need content ");
//            return false;
//        }
        return true;
    }

    @Override
    public boolean validateMessage(PushMessage pushMessage) {
        //基本业务检查由validation进行 此处进行复杂检查
        //渠道不能为空
        WeChatMessageProtocol origin = pushMessage.getOrigin();
        if (origin.getPushChannel() == null) {
            logger.error("push channel is null");
            return false;
        } else {
            if (origin.getPushChannel().equals(PUSH_CHANNEL.TEST)) {
                return true;
            }
            if (origin.getReceiver().getPlatform().equals(PUSH_PLATFORM.WC_PUBLIC)) {
                return checkWebChatPublicMessageProtocol(origin);
            } else {
                logger.error("channel not support now!");
                return false;
            }
        }
    }
}
